//
//  FormViewController.m
//  etionUI
//
//  Created by wangjian on 12/24/13.
//  Copyright (c) 2013 GuangZhouXuanWu. All rights reserved.
//

#import "FormViewController.h"
#import "ResizeableFormView.h"

@interface CFormViewController () <CResizeableFormViewDelegate, CFreezeFormViewDelegate>

@end

@implementation CFormViewController
{
    NSUInteger number;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    CResizeableFormView *freezeFormView = [[CResizeableFormView alloc] initWithFrame:CGRectMake(0, 20, self.view.width, self.view.height-20)];
    freezeFormView.delegate = self;
    freezeFormView.rowNum = 200;
    freezeFormView.colNum = 10;
    freezeFormView.freezeTopRowNum = 1;
    freezeFormView.freezeColNum = 1;
    freezeFormView.freezeBottomRowNum = 1;
    [self.view addSubview:freezeFormView];
    
    self.view.backgroundColor = [UIColor whiteColor];
}

#pragma mark form view delegate

//- (NSUInteger)numberOfColsWithFormView:(CFreezeFormView *)view
//{
//    return 5;
//}
//
//- (NSUInteger)numberOfRowsWithFormView:(CFreezeFormView *)view
//{
//    return 15;
//}

//- (CGFloat)freezeFormView:(CFreezeFormView *)view heightForRow:(NSUInteger)row
//{
//    if (row == 0)
//    {
//        return 60;
//    }
//    if (row == 1)
//    {
//        return 32;
//    }
//    return 34;
//}
//
//- (CGFloat)freezeFormView:(CFreezeFormView *)view widthForCol:(NSUInteger)col
//{
//    if (col == 0)
//    {
//        return 50;
//    }
//    if (col == 1)
//    {
//        return 40;
//    }
//    return 80;
//}

- (CFormCellView *)freezeFormView:(CFreezeFormView *)view cellAtIndex:(CFormCellIndex *)index
{
    CFormCellView *cell = nil;
    if (index.row < view.freezeTopRowNum)
    {
        cell = [view dequeueReusableCellWithIdentifier:@"headTitleCell" forIndex:index];
        if (cell == nil)
        {
            cell = [[[CFormCellView alloc] initWithReuseIdentifier:@"headTitleCell"] autorelease];
            cell.textLable.textColor = [UIColor whiteColor];
            cell.textLable.textAlignment = NSTextAlignmentCenter;
            cell.textLable.font = [UIFont boldSystemFontOfSize:DEF_FORM_CELL_FONT_SIZE];
        }
        cell.textLable.text = [NSString stringWithFormat:@"%@", [index description]];
    }
    else if (index.col == 1)
    {
        cell = [view dequeueReusableCellWithIdentifier:@"dropdownCell" forIndex:index];
        if (cell == nil)
        {
            cell = [[[CFormDropDownCellView alloc] initWithReuseIdentifier:@"dropdownCell"] autorelease];
        }
        cell.textLable.text = [NSString stringWithFormat:@"%@", [index description]];
    }
    else if (index.col == 2)
    {
        cell = [view dequeueReusableCellWithIdentifier:@"iconCell" forIndex:index];
        if (cell == nil)
        {
            cell = [[[CFormCellView alloc] initWithReuseIdentifier:@"iconCell"] autorelease];
            cell.iconView.image = [UIImage imageNamed:@"rtctrl_barcode"];
        }
    }
    else if (index.col == 3)
    {
        cell = [view dequeueReusableCellWithIdentifier:@"linkCell" forIndex:index];
        if (cell == nil)
        {
            cell = [[[CFormLinkCellView alloc] initWithReuseIdentifier:@"linkCell"] autorelease];
        }
        ((CFormLinkCellView *)cell).text = [NSString stringWithFormat:@"%@", [index description]];
    }
    else if (index.col == 4)
    {
        cell = [view dequeueReusableCellWithIdentifier:@"textInputCell" forIndex:index];
        if (cell == nil)
        {
            cell = [[[CFormTextInputCellView alloc] initWithReuseIdentifier:@"textInputCell"] autorelease];
        }
        cell.textLable.text = [NSString stringWithFormat:@"%@", [index description]];
    }
    else if (index.col == 5)
    {
        cell = [view dequeueReusableCellWithIdentifier:@"imageCell" forIndex:index];
        if (cell == nil)
        {
            cell = [[[CFormImageCellView alloc] initWithReuseIdentifier:@"imageCell"] autorelease];
        }
        ((CFormImageCellView *)cell).imageView.image = [UIImage imageNamed:@"logo"];
    }
    else if (index.col == 6)
    {
        cell = [view dequeueReusableCellWithIdentifier:@"callCell" forIndex:index];
        if (cell == nil)
        {
            cell = [[[CFormCallCellView alloc] initWithReuseIdentifier:@"callCell"] autorelease];
        }
        cell.textLable.text = [NSString stringWithFormat:@"%@", [index description]];
    }
    else if (index.col == 7)
    {
        cell = [view dequeueReusableCellWithIdentifier:@"selectorCell" forIndex:index];
        if (cell == nil)
        {
            cell = [[[CFormSelectorCellView alloc] initWithReuseIdentifier:@"selectorCell"] autorelease];
        }
        cell.textLable.text = [NSString stringWithFormat:@"%@", [index description]];
    }
    else
    {
        cell = [view dequeueReusableCellWithIdentifier:@"textCell" forIndex:index];
        if (cell == nil)
        {
            cell = [[[CFormCellView alloc] initWithReuseIdentifier:@"textCell"] autorelease];
        }
        cell.textLable.text = [NSString stringWithFormat:@"%@", [index description]];
    }

    return cell;
}

- (void)freezeFormView:(CFreezeFormView *)view didClickCellAtIndex:(CFormCellIndex *)index
{
    if (index.row >= view.freezeTopRowNum)
    {
        CFormCellView *cell = (CFormLinkCellView *)[view cellAtIndex:index];
        if ([cell isKindOfClass:[CFormLinkCellView class]])
        {
            CFormLinkCellView *linkCell = (CFormLinkCellView *)cell;
            if (linkCell.hasClicked == NO)
            {
                linkCell.hasClicked = YES;
            }
        }
        else if ([cell isKindOfClass:[CFormSelectorCellView class]])
        {
            CFormSelectorCellView *selectorCell = (CFormSelectorCellView *)cell;
            selectorCell.chosed = !selectorCell.chosed;
        }
    }
}

@end
